package com.rxjava;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public class MainActivity extends AppCompatActivity {

    String tag = "RxJava";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //test1();
        //test2();
        //test3();
        //test4();
        //test5();
        //test6();
        testMap();
    }

    public void test1() {
        Observable observable = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e) throws Exception {
                Log.e(tag, "Observable  1234");
                //e.onNext("1234");
                //e.onComplete();
            }
        });
        Observer observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(tag, "Observer   onSubscribe   ------------------------------001");
            }

            @Override
            public void onNext(String s) {
                Log.e(tag, "Observer   onNext   " + s);
            }

            @Override
            public void onError(Throwable e) {
                Log.e(tag, "Observer   onError   ");
            }

            @Override
            public void onComplete() {
                Log.e(tag, "Observer   onComplete   ");
            }
        };
        observable.subscribe(observer);
    }


    public void test2() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                Log.e(tag, "Observable  456");
                //e.onNext("456");
                //e.onError(null);
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(tag, "Observer   onSubscribe   ------------------------------002");
            }

            @Override
            public void onNext(String s) {
                Log.e(tag, "Observer   onNext   " + s);
            }

            @Override
            public void onError(Throwable e) {
                Log.e(tag, "Observer   onError   ");
            }

            @Override
            public void onComplete() {
                Log.e(tag, "Observer   onComplete   ");
            }
        });
    }


    public void test3() {
        Observable.just("123", "456", "789").subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String s) {
                Log.e(tag, "just onNext  " + s);
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });
    }

    public void test4() {
        Observable.fromArray(new Integer[]{1, 2, 3, 4}).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Integer integer) {
                Log.e(tag, "onNext" + integer);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                Log.e(tag, "onComplete" );
            }
        });
    }

    public void test5() {
        //适合 调用某方法  执行完后 不需要返回参数 比如 数据更新后， 需要从新渲染UI   回调onComplete  中 写invalidate();
        Observable.empty().subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Object o) {
                Log.e(tag, "onNext");
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
                Log.e(tag, "onNext");
            }
        });
    }

    public void test6(){
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> i) throws Exception {
                SystemClock.sleep(3000);
                i.onNext(3);
                SystemClock.sleep(3000);
                i.onNext(5);
                SystemClock.sleep(3000);
                i.onNext(7);
                i.onComplete();
            }
        }).map(new Function<Integer,String>() {
            @Override
            public String apply(Integer i) throws Exception {
                return i+"";
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(tag, "onSubscribe");
            }
            @Override
            public void onNext(String s) {
                Log.e(tag, "onNext  "+s);
            }
            @Override
            public void onError(Throwable e) {
                Log.e(tag, "onError  ");
            }
            @Override
            public void onComplete() {
                Log.e(tag, "onComplete  ");
            }
        });
    }



    //Map测试
    public void testMap(){
        //原
        //        Observable.just("headr.png","bit.png").map(new Function<String, Integer>() {
        //            @Override
        //            public Integer apply(String url) throws Exception {
        //                //""进行网络请求
        //                return 1;
        //            }
        //        }).subscribe(new Observer<Integer>() {
        //            @Override
        //            public void onSubscribe(Disposable d) {
        //                Log.e(tag, "onSubscribe  ");
        //            }
        //
        //            @Override
        //            public void onNext(Integer bitmap) {
        //                Log.e(tag, "onNext  ");
        //            }
        //
        //            @Override
        //            public void onError(Throwable e) {
        //                Log.e(tag, "onError  ");
        //            }
        //
        //            @Override
        //            public void onComplete() {
        //                Log.e(tag, "onComplete  ");
        //            }
        //        });

        //int ->String
        //        Observable.just(1000,1001).map(new Function<Integer, String>() {
        //            @Override
        //            public String apply(Integer id) throws Exception {
        //                //""进行网络请求
        //                return "http"+id;
        //            }
        //        }).subscribe(new Observer<String>() {
        //            @Override
        //            public void onSubscribe(Disposable d) {
        //                Log.e(tag, "onSubscribe  ");
        //            }
        //            @Override
        //            public void onNext(String string) {
        //                Log.e(tag, "onNext  "+string);
        //            }
        //            @Override
        //            public void onError(Throwable e) {
        //                Log.e(tag, "onError  ");
        //            }
        //            @Override
        //            public void onComplete() {
        //                Log.e(tag, "onComplete  ");
        //            }
        //        });

        //int ->String
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                Log.e(tag, "create  subscribe");
                e.onNext(1000000);
                e.onNext(1000001);
                e.onNext(1000002);
            }
        }).map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Exception {
                Log.e(tag, "apply  ");
                return ""+integer;
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e(tag, "onSubscribe  ");
            }
            @Override
            public void onNext(String string) {
                Log.e(tag, "onNext  -----------------------  "+string);
            }
            @Override
            public void onError(Throwable e) {
                Log.e(tag, "onError  ");
            }
            @Override
            public void onComplete() {
                Log.e(tag, "onComplete  ");
            }
        });
    }
}